package com.bidevalution.controller.api;

import com.bidevalution.business.service.*;
import com.bidevalution.controller.util.RequestUtil;
import com.bidevalution.dao.entity.BeSectionEnterprise;
import com.bidevalution.support.context.Constants;
import com.qtp.common.core.TSUser;
import com.qtp.common.data.RedisAccess;
import com.qtp.core.model.Response;
import com.qtp.core.redis.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: xiayf
 * @date: 2020-01-03 8:45
 */
@Log4j2
@RestController
@RequestMapping("/beSectionStepJob")
@Api(description = "标段环节任务相关操作Controller")
public class BeSectionStepJobController {

    @Autowired
    private BeSectionStepJobService beSectionStepJobService;
    @Autowired
    private BeSectionEnterpriseService beSectionEnterpriseService;
    @Autowired(required = false)
    private BeSectionReportService beSectionReportService;
    @Autowired(required = false)
    private BeSectionInfoService beSectionInfoService;

    private static String ADVANCE_EXECUTE_JOB_RUNNING = "ADVANCE_EXECUTE_JOB_RUNNING";

    @GetMapping("/queryUnFinish")
    @ApiOperation(value = "查询计算还未执行完成任务", notes = "下一步判断", response = String.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sectionStepId", value = "环节id", required = true, dataType = "String", paramType = "query")})
    public Response<String> queryUnFinish(@RequestParam("sectionId") String sectionId, @RequestParam("sectionStepId") String sectionStepId) {
        try {
            String userGuid = RequestUtil.getHeader("userGuid");
            TSUser tsUser = RedisAccess.getValue(TSUser.class, userGuid);
            if (tsUser == null) {
                return Response.fail("获取账号登录信息失败！");
            }
            if (StringUtils.isBlank(sectionId)) {
                return Response.fail("参数不可以为空！");
            }
            List<BeSectionEnterprise> enterList = beSectionEnterpriseService.queryEnterpriseByIdAndPrice(sectionId);
            if (enterList.size() == Constants.ZERO) {
                return Response.ok("二阶段项目投标文件还未导入，请先导入！");
            }
            if (beSectionStepJobService.findUnFinishList(sectionId).size() > 0) {
                return Response.ok("评审流程提前计算的任务未完成，请等待计算完成再操作！");
            }
            Map<String, Object> params = new HashMap<>(8);
            params.put("sectionId", sectionId);
            params.put("reportExpert1", 0);
            params.put("reportExpert2", -1);
            beSectionReportService.updateReportExpert(params);
            /*BeSectionMethod beSectionMethod = beSectionMethodService.selectBySectionId(sectionId);
            //设计预审和设计后审方法需要在这里计算基准价
            if (EvalutionMethodEnum.SJZH_ZGYS.getCode().equals(beSectionMethod.getMethodCode()) ||
                    EvalutionMethodEnum.SJZH_ZGHS.getCode().equals(beSectionMethod.getMethodCode())) {
                BeSectionStep beSectionStep = beSectionStepService.selectByPrimaryKey(sectionStepId);
                BeSectionExpertInfo beSectionExpertInfo = beSectionExpertInfoService
                        .selectByLoginName(beSectionStep.getSectionId(), tsUser.getLoginName());
                //执行当前环节任务
                beSectionStepJobService.callStepJob(beSectionStep, beSectionMethod.getSubmitCode(), StepStage.Before,
                        beSectionExpertInfo.getSeqId(), null);
            }*/
            return Response.ok(Constants.STR_YES);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("系统异常，请联系管理员！");
        }
    }

    @PostMapping("/updateStepJobBySectionId")
    @ApiOperation(value = "根据标段id更新环节任务状态", notes = "提前计算", response = String.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "sectionId", required = true, dataType = "String", paramType = "query")})
    public Response<String> updateStepJobBySectionId(@RequestParam("sectionId") String sectionId) {
        try {
            if (StringUtils.isBlank(sectionId)) {
                return Response.fail("参数不可以为空！");
            }
            //评标还未开始不能点击二阶段环节计算按钮，计算过程中不能重复点击
            Integer reviewState = beSectionInfoService.getBeSectionInfoById(sectionId).getReviewState();
            if (!Constants.TWO.equals(reviewState)) {
                return Response.fail("评标还未开始或评标已结束，请勿点击！");
            }
            if (Constants.TRUE.equals(RedisUtil.get(ADVANCE_EXECUTE_JOB_RUNNING))) {
                return Response.fail("正在计算，请勿重复点击！");
            }
            RedisUtil.set(ADVANCE_EXECUTE_JOB_RUNNING, Constants.TRUE);
            RedisUtil.expire(ADVANCE_EXECUTE_JOB_RUNNING, 60 * 60);
            beSectionStepJobService.updateStepJobBySectionId(sectionId);
            RedisUtil.delete(ADVANCE_EXECUTE_JOB_RUNNING);
            return Response.ok(Constants.STR_YES);
        } catch (Exception e) {
            RedisUtil.delete(ADVANCE_EXECUTE_JOB_RUNNING);
            log.error(e.getMessage(), e);
            return Response.fail("系统异常，请联系管理员！");
        }
    }

}
